const tryCatch = require('../../utils/tryCatch');
const {
    ServerFindCommendFriends,
    ServerMakeFriend,
    ServerMakeFindFriends,
    ServerGetApplyFriends,
    ServerRejectFriends,
    ServerConfirmFirends,
    ServerGetFriendsList,
    ServerGetFriendsDetil,
    ServerFriendsCommend,
    ServerDeleteFriends,
    ServerSearchFriends,
    ServerGetFriends,
    ServerUpdataFriendMessageId,
    GetFriend
} = require("../../server/friends/index")
const { ServerGetUserInfo, ServerGetSocketIdByUserId } = require("../../server/user/index")
const { Private } = require("../../server/message")
const socket = require("../socket/initSocket")
const Friends = {
    RecommendFriend: tryCatch(async (req, res) => {
        const ServerGetUserInfores = await ServerGetUserInfo(req.auth.userAccount)
        if (ServerGetUserInfores.isok) {
            const FindCommendFriendsres = await ServerFindCommendFriends(ServerGetUserInfores.data.id)
            if (FindCommendFriendsres.isok) {
                res.send({
                    statusCode: 200,
                    errorInfo: "获取成功",
                    data: FindCommendFriendsres.data
                })
            } else {
                res.send({
                    statusCode: 200,
                    errorInfo: "获取成功",
                    data: FindCommendFriendsres.data
                })
            }
        } else {
            res.send({
                statusCode: 403,
                errorInfo: "获取失败",
                data: ServerGetUserInfores.data
            })
        }
    }),
    AddFriends: tryCatch(async (req, res) => {
        const MakeFindFriends = await ServerMakeFindFriends({ id1: req.body.id1, id2: req.body.id2 })
        if (MakeFindFriends.isok) {
            const MakeFriendres = await ServerMakeFriend({
                userId: req.body.id1,
                friendUserId: req.body.id2,
                commentInfo: req.body.commentInfo || "",
                isConfirm: 3
            })
            if (MakeFriendres.isok) {
                //查询对方的 userid
                ServerGetSocketIdByUserId(req.body.id2).then(async (res) => {
                    if (res.isok) {
                        const UserInfores = await ServerGetUserInfo(req.body.id1, true)
                        if (UserInfores.isok) {
                            console.log("userId " + req.body.id1 + " 向 " + req.body.id2 + "发送好友请求");
                            socket.io.to(res.data.socketid).emit("addFriend", {
                                userId: req.body.id1,
                                commentInfo: req.body.commentInfo || "",
                                userAvatar: UserInfores.data.userAvatar,
                                userName: UserInfores.data.userName,
                                userSex: UserInfores.data.userSex
                            })
                        }
                    }
                })

                res.send({
                    statusCode: 200,
                    data: MakeFriendres.data
                })
            } else {
                res.send({
                    statusCode: 403,
                    errorInfo: "添加失败",
                    data: MakeFriendres.data
                })
            }
        } else {
            res.send({
                statusCode: 403,
                errorInfo: MakeFindFriends.data.isConfirm == 1 ? "已添加了此好友" : "已经发送好友请求等待对方回应吧",
                data: {}
            })
        }
    }),
    GetApplyfriends: tryCatch(async (req, res) => {
        const ServerGetUserInfores = await ServerGetUserInfo(req.auth.userAccount)
        if (ServerGetUserInfores.isok) {
            const GetApplyFriendsres = await ServerGetApplyFriends(ServerGetUserInfores.data.id)
            if (GetApplyFriendsres.isok) {
                res.send({
                    statusCode: 200,
                    data: GetApplyFriendsres.data
                })
            } else {
                res.send({
                    statusCode: 200,
                    errorInfo: GetApplyFriendsres.message,
                    data: []
                })
            }
        } else {
            res.send({
                statusCode: 403,
                errorInfo: "获取失败",
                data: ServerGetUserInfores.data
            })
        }
    }),
    Rejectfriends: tryCatch(async (req, res) => {
        const RejectFriends = await ServerRejectFriends(req.body.userId, req.body.friendUserId)
        if (RejectFriends.isok) {
            res.send({
                statusCode: 200,
                data: RejectFriends.data
            })
        } else {
            res.send({
                statusCode: 403,
                errorInfo: RejectFriends.message,
                data: {}
            })
        }
    }),
    ConfirmFriends: tryCatch(async (req, res) => {
        const ConfirmFirendsres = await ServerConfirmFirends({ userId: req.body.userId, friendUserId: req.body.friendUserId, commendName: req.body.commendName })
        if (ConfirmFirendsres.isok) {
            const getFriendres = await ServerGetFriends({ userId: req.body.userId, friendUserId: req.body.friendUserId })
            if (getFriendres.isok) {
                Private.ServersetMessage({
                    messageId: getFriendres.data.friendMessageId,
                    messageContent: "双方已经是好友快开始聊天吧",
                    messageSendType: 0,
                    messageType: "data"
                }).then((setMessageres) => {
                    ServerUpdataFriendMessageId(setMessageres.data.id, req.body.userId, req.body.friendUserId)
                    ServerGetSocketIdByUserId(req.body.userId).then(async res => {
                        if (res.isok) {
                            const UserInfores = await ServerGetUserInfo(req.body.friendUserId, true)
                            console.log("userId " + req.body.friendUserId + " 同意了 " + req.body.userId + "的好友请求");
                            socket.io.to(res.data.socketid).emit("confirmFriend", {
                                id: setMessageres.data.id,
                                message: "双方已经是好友快开始聊天吧",
                                messageType: "data",
                                avatar: UserInfores.data.userAvatar,
                                name: getFriendres.data.commendName1 ? getFriendres.data.commendName1 : UserInfores.data.userName,
                                lastMessageTime: setMessageres.data.createdAt,
                                noMessageNum: getFriendres.data.noMessageNum2,
                                type: "friend"
                            })
                        }
                    })
                })
            }
            res.send({
                statusCode: 200,
                data: ConfirmFirendsres.data
            })
        } else {
            res.send({
                statusCode: 403,
                errorInfo: ConfirmFirendsres.message,
                data: ConfirmFirendsres.data
            })
        }
    }),
    Getfriendslist: tryCatch(async (req, res) => {
        if (req.query.userId) {
            const GetFriendsListres = await ServerGetFriendsList(req.query.userId)
            if (GetFriendsListres.isok) {
                res.send({
                    statusCode: 200,
                    data: GetFriendsListres.data,
                    message: GetFriendsListres.message
                })
            } else {
                res.send({
                    statusCode: 403,
                    errorInfo: GetFriendsListres.message,
                    data: GetFriendsListres.data
                })
            }
        } else {
            res.send({
                statusCode: 403,
                errorInfo: "参数错误",
                data: false
            })
        }
    }),
    GetfriendDetil: tryCatch(async (req, res) => {
        if (req.query.userId && req.query.friendUserId) {
            const GetFriendsDetilres = await ServerGetFriendsDetil(req.query.userId, req.query.friendUserId)
            if (GetFriendsDetilres.isok) {
                res.send({
                    statusCode: 200,
                    data: GetFriendsDetilres.data,
                    message: GetFriendsDetilres.message
                })
            } else {
                res.send({
                    statusCode: 403,
                    errorInfo: GetFriendsDetilres.message,
                    data: GetFriendsDetilres.data
                })
            }
        } else {
            res.send({
                statusCode: 403,
                errorInfo: "参数错误",
                data: false
            })
        }

    }),
    ChangeFriendsCommend: tryCatch(async (req, res) => {
        const commendName = req.body.commendName ?? ""
        const ServerFriendsres = await ServerFriendsCommend(commendName, req.body.userId, req.body.friendUserId);
        if (ServerFriendsres.isok) {
            res.send({
                statusCode: 200,
                data: ServerFriendsres.data,
                message: ServerFriendsres.message
            })
        } else {
            res.send({
                statusCode: 403,
                errorInfo: ServerFriendsres.message,
                data: ServerFriendsres.data
            })
        }
    }),
    DeleteFriend: tryCatch(async (req, res) => {
        if (req.body.userId && req.body.friendUserId) {
            const DeleteFriendsres = await ServerDeleteFriends(req.body.userId, req.body.friendUserId)
            if (DeleteFriendsres.isok) {
                res.send({
                    statusCode: 200,
                    data: DeleteFriendsres.data,
                    message: DeleteFriendsres.message
                })
            } else {
                res.send({
                    statusCode: 403,
                    errorInfo: DeleteFriendsres.message,
                    data: DeleteFriendsres.data
                })
            }
        } else {
            res.send({
                statusCode: 403,
                errorInfo: "参数错误",
                data: false
            })
        }
    }),
    Getlist: tryCatch(async (req, res) => {
        if (req.body.userId && req.body.searchvalue) {
            const SearchFriendsres = await ServerSearchFriends(req.body.userId, req.body.searchvalue)
            if (SearchFriendsres.isok) {
                res.send({
                    statusCode: 200,
                    data: SearchFriendsres.data,
                    message: SearchFriendsres.message
                })
            } else {
                res.send({
                    statusCode: 403,
                    errorInfo: SearchFriendsres.message,
                    data: SearchFriendsres.data
                })
            }
        } else {
            res.send({
                statusCode: 403,
                errorInfo: "参数错误",
                data: false
            })
        }
    }),
    Userdetil: tryCatch(async (req, res) => {
        if (req.body.userId && req.body.messageId) {
            const Friendres = await GetFriend(req.body.userId, req.body.messageId)
            if (Friendres.isok) {
                res.send({
                    statusCode: 200,
                    data: Friendres.data,
                    message: Friendres.message
                })
            } else {
                res.send({
                    statusCode: 403,
                    errorInfo: Friendres.message,
                    data: Friendres.data
                })
            }
        } else {
            res.send({
                statusCode: 403,
                errorInfo: "参数错误",
                data: false
            })
        }
    }),
}

module.exports = Friends